package com.gasolinator.a.app.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.gasolinator.a.R;
import com.gasolinator.a.app.GasolinatorData;
import com.gasolinator.a.app.GasolinatorStation;
import com.gasolinator.a.app.helper.NumberFormats;
import com.gasolinator.a.augmented_reality.data.GasolinatorDataSource;
import com.gasolinator.a.augmented_reality.ui.GasolinatorMarker;
import com.gasolinator.a.augmented_reality.ui.Marker;
import org.json.JSONException;

import java.util.List;

public class GasolinatorStationAdapter extends BaseAdapter
{
    String combustible;
    List<Marker> gasolinatorMarkers;
    private Context mContext;
    int type;
    int viewId;

    public GasolinatorStationAdapter(Context context, int type) {
        mContext = context;
        this.type = type;
        GasolinatorDataSource gasolinatorDataSource = GasolinatorDataSource.getInstance(context);
        //if (gasolinatorDataSource.getMarkers()==null) return;
        gasolinatorDataSource.sort(type);
        gasolinatorMarkers = gasolinatorDataSource.getGasolinatorMarkersFilteredByPrefs(context);
        if (type==GasolinatorData.FAVORITES) {
            gasolinatorMarkers = gasolinatorDataSource.getGasolinatorMarkersFavorites(context,gasolinatorMarkers);
        }

        viewId = R.layout.gasolinator_list_adapter;

    }

    public int getCount()
    {
        if (gasolinatorMarkers==null) return 0;
        return this.gasolinatorMarkers.size();
    }

    public Object getItem(int paramInt)
    {
        return Integer.valueOf(paramInt);
    }

    public long getItemId(int paramInt)
    {
        return paramInt;
    }

    public GasolinatorStation getObject(int paramInt) throws JSONException
    {
        return ((GasolinatorMarker)gasolinatorMarkers.get(paramInt)).getStation();
    }

    public View getView(int paramInt, View paramView, ViewGroup viewGroup )
    {
        LayoutInflater localLayoutInflater = LayoutInflater.from(mContext);
        LinearLayout localLinearLayout = (LinearLayout)localLayoutInflater.inflate(viewId, viewGroup, false);

        try
        {
            GasolinatorStation localGasStation = ((GasolinatorMarker)gasolinatorMarkers.get(paramInt)).getStation();

            ImageView localImageView = (ImageView)localLinearLayout.findViewById(R.id.BrandLogo);
            localImageView.setImageResource(localGasStation.getImageResource());
            TextView textViewName = (TextView)localLinearLayout.findViewById(R.id.Name);
            textViewName.setText(localGasStation.getName());

            TextView textViewAddress = (TextView)localLinearLayout.findViewById(R.id.Address);
            textViewAddress.setText(localGasStation.getAddress());

            if (type==0) { //preu
            TextView textviewDistance = (TextView)localLinearLayout.findViewById(R.id.Distance);
            String kmString = String.valueOf(NumberFormats.distanceFormat(localGasStation.getDistance()));
            textviewDistance.setText(kmString);

            TextView textViewPrice = (TextView)localLinearLayout.findViewById(R.id.Price);
            String priceString = String.valueOf(NumberFormats.priceFormat(Double.valueOf(localGasStation.getPrice(combustible)))).concat(" €/L");
            textViewPrice.setText(priceString);
            }
            else {
            TextView textviewDistance = (TextView)localLinearLayout.findViewById(R.id.Price);
            String kmString = String.valueOf(NumberFormats.distanceFormat(localGasStation.getDistance()));
            textviewDistance.setText(kmString);

            TextView textViewPrice = (TextView)localLinearLayout.findViewById(R.id.Distance);
            String priceString = String.valueOf(NumberFormats.priceFormat(Double.valueOf(localGasStation.getPrice(combustible)))).concat(" €/L");
            textViewPrice.setText(priceString);

            }

        }

        catch (Exception localException)
        {
            localException.printStackTrace();
        }

        return localLinearLayout;
    }

    public void setCombustible(String paramString)
    {
        this.combustible = paramString;
    }
}